﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

namespace Danser_AJAXEnabledWebApplication.User_Controls
{
    /// <summary>
    /// List enabling multi item selection
    /// </summary>
    public partial class MultiSelectList : System.Web.UI.UserControl
    {
        #region Constants:

        private const String OPEN_LABEL = "Select";
        private const String CLOSE_LABEL = "Close";

        private const String OPEN_TOOLTIP = "Open selection list";
        private const String CLOSE_TOOLTIP = "Save selection";

        #endregion

        #region Page Load:

        /// <summary>
        /// Loads the page...
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        #endregion

        #region Control Handlers:
 
        /// <summary>
        /// handles the event of the select button was clicked
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void selectButton_Click(object sender, EventArgs e)
        {
            //toggle controls appearance
            checkBoxPanel.Visible = !checkBoxPanel.Visible;
            SwitchLabel(selectButton);
        }

        #endregion

        #region Heplers:

        /// <summary>
        /// Toggle text of give UI button
        /// </summary>
        /// <param name="button">the button to switch text</param>
        private void SwitchLabel(Button button)
        {
            if (button.Text.Equals(OPEN_LABEL))
            {
                button.Text = CLOSE_LABEL;
                button.ToolTip = CLOSE_TOOLTIP;
            }
            else
            {
                button.Text = OPEN_LABEL;
                button.ToolTip = OPEN_TOOLTIP;
            }
        }

        #endregion

        #region Public Properties:

        /// <summary>
        /// Get all selected items
        /// </summary>
        public ListItemCollection SelectedItems
        {
            get 
            {
                ListItemCollection collection = new ListItemCollection();

                foreach (ListItem item in checkBoxList.Items)
                {
                    if (item.Selected)
                        collection.Add(item);
                }
                return collection;
            }
        }

        /// <summary>
        /// Get all items
        /// </summary>
        public ListItemCollection Items
        {
            get { return checkBoxList.Items; }
        }

        #endregion

        #region Public Functionality:

        /// <summary>
        /// deselect all list items
        /// </summary>
        public void ClearSelected()
        {
            foreach (ListItem item in checkBoxList.Items)
                item.Selected = false;
        }

        /// <summary>
        /// Select all list items
        /// </summary>
        public void SelectAll()
        {
            foreach (ListItem item in checkBoxList.Items)
                item.Selected = true;
        }

        /// <summary>
        /// Hide the items list
        /// </summary>
        public void HideSelectionList()
        {
            checkBoxPanel.Visible = false;
            selectButton.Text = OPEN_LABEL;
            selectButton.ToolTip = OPEN_TOOLTIP;
        }

        #endregion

    }
}